package nahuel.games.carracer;

import nahuel.games.carracer.Juego.Personaje;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;

public class Auto{
	protected GameView gameView;
	protected Bitmap bmp;

	public static final int VEL_DETENIDO = 0;
	public static final int VEL_LENTO = 10;
	public static final int VEL_CRUCERO = 40;
	public static final int VEL_RAPIDA = 80;
	public static final int VEL_MAXIMA = 120;
	public static final int VEL_EXTRA = 140;

	protected int x = 0;
	protected int y = 0;
	protected int velX = 0;
	protected int velY = VEL_CRUCERO;
	protected int acelY = 0;
	protected float rotacion = 0;
	protected int width;
	protected int height;
	protected int altoD;
	protected int anchoD;
	protected int imagen;
	Personaje personaje;
	protected Vida vida;

	protected static final int MIN_ALTO_PANT = 480;
	protected static final int BASE_TAM_AUTO = 60;
	protected static final int FACTOR_TAM_AUTO = 15;
	static final String POS_X = "posX";
	static final String POS_Y = "posY";
	static final String VEL_X = "velX";
	static final String VEL_Y = "velY";
	static final String VIDA = "vida";

	public Auto(GameView vista, Personaje pers, int anchoD, int altoD) {
		this.anchoD = anchoD;
		this.altoD = altoD;
		this.gameView = vista;
		
		this.personaje = pers;
		switch(personaje){
		case policia:
			this.imagen = R.drawable.policia;
			break;
		case ladron:
			this.imagen = R.drawable.ladron;
			break;
		case trafico:
			this.imagen = R.drawable.trafico;
			break;
		}
		redimensionar();
		this.vida = new Vida(width);
		posInicial();
	}

	private void redimensionar() {
		synchronized (gameView.getHolder()) {
			int aux = 0;
			if(altoD > MIN_ALTO_PANT)
				aux = BASE_TAM_AUTO - altoD / FACTOR_TAM_AUTO;
			int altoAuto = BASE_TAM_AUTO + aux;
			this.bmp = GameView.decodeSampledBitmap(gameView.getResources(),
					imagen, 0, altoAuto);
			this.width = bmp.getWidth();
			this.height = bmp.getHeight();
		}
	}
	
	public boolean chequeoLimites(){
		synchronized (gameView.getHolder()) {
			if (x < anchoD * 8 / 45) {
				x = anchoD * 8 / 45;
			}
			if (x > (anchoD * 37 / 45) - width) {
				x = (anchoD * 37 / 45) - width;
			}
			if (y > altoD - height) {
				y = altoD - height;
			}

			if (y < altoD / 2) {
				y = altoD / 2;
				return true;
			}
		}
		return false;
	}

	public void posInicial() {
		synchronized (gameView.getHolder()) {
			x = (anchoD / 2) - (width / 2); // centro de la pantalla
			y = altoD - (height * 2); // base de la pantalla
		}
	}

	public void draw(Canvas canvas) {
		canvas.save();
		canvas.rotate(rotacion, x + width / 2, y + height / 2);
		canvas.drawBitmap(bmp, x, y, null);
		canvas.restore();
	}

	public void update() {
		synchronized (gameView.getHolder()) {
			x += gameView.escalarVelocidadX(velX);
			velY += acelY;
			if (velY < VEL_DETENIDO) {
				this.velY = VEL_DETENIDO;
			}
			if (velY > VEL_MAXIMA) {
				this.velY = VEL_MAXIMA;
			}
		}
		chequeoLimites();
	}

	public void acelerar(int x, int y) {
		synchronized (gameView.getHolder()) {
			this.velX = x;
			rotacion = x / 2;
			this.acelY = y;
		}
	}

	public int getVelocidadY(){
		return velY;
	}
	
	public int getVelocidadX(){
		return velX;
	}

	public Personaje getPersonaje(){
		return personaje;
	}
	
	public boolean hayColision(Auto otro){
		return ( (x < otro.x+otro.width) && (x+width > otro.x) &&
				 (y < otro.y+otro.height) && (y+height > otro.y)
				);
	}

	// Para override en los hijos
	public void choque(Auto otro) {
	}

	// Para override en los hijos
	public void setVelocidadRelativa(int i, int j) {
	}

	public Vida getVida(){
		vida.setX(x);
		vida.setY(y);
		return vida;
	}

	public Bundle getBundle() {
		Bundle bund = new Bundle();
		bund.putInt(POS_X, x);
		bund.putInt(POS_Y, y);
		bund.putInt(VEL_X, velX);
		bund.putInt(VEL_Y, velY);
		bund.putBundle(VIDA, vida.getBundle());
		return bund;
	}

	public void setBundle(Bundle bundle) {
		x = bundle.getInt(POS_X);
		y = bundle.getInt(POS_Y);
		velX = bundle.getInt(VEL_X);
		velY = bundle.getInt(VEL_Y);
		vida.setBundle(bundle.getBundle(VIDA));
	}

}
